package com.example.notepad;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.notepad.adapter.NotepadAdapter;
import com.example.notepad.bean.Notepad;
import com.example.notepad.database.SQLiteHelper;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<Notepad> notepads;
    private RecyclerView rvNotepads;
    private SQLiteHelper helper;
    private NotepadAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rvNotepads = findViewById(R.id.recyclerview);
        rvNotepads.setLayoutManager(new LinearLayoutManager(this));
        rvNotepads.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        ImageView ivAdd = findViewById(R.id.btn_add);
        ivAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, RecordActivity.class);
                startActivityForResult(intent, 1);
            }
        });
        initData();
    }

    private void initData() {
        helper = new SQLiteHelper(this);
        showQueryData();
    }

    private void showQueryData() {
        if(notepads != null) {
            notepads.clear();
        }
        notepads = helper.query();
        adapter = new NotepadAdapter(notepads);
        rvNotepads.setAdapter(adapter);
        adapter.setOnItemClickListener(new NotepadAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Notepad notepad = notepads.get(position);
                Intent intent = new Intent(MainActivity.this, RecordActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("note", notepad);
                intent.putExtras(bundle);
                MainActivity.this.startActivityForResult(intent, 1);
            }
        });
        adapter.setOnItemLongClickListener(new NotepadAdapter.OnItemLongClickListener() {
            @Override
            public void onItemLongClick(View view, final int position) {
                new AlertDialog.Builder(MainActivity.this)
                        .setMessage("是否删除此记录")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Notepad notepad = notepads.get(position);
                                if (helper.delete(String.valueOf(notepad.getId()))) {
                                    notepads.remove(position);
                                    adapter.notifyItemRemoved(position);
                                    showToast("删除成功");
                                }
                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1 && resultCode == RESULT_OK) {
            showQueryData();
        }
    }
    private void showToast(String msg) {
        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
    }

}